#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

int main() {
	
	int n,m;
	cin>>n>>m;
	string s1,s2;
	cin>>s1>>s2;
	s1=" "+s1;
	s2=" "+s2;
	vector<vector<int>> dp(n+1,vector<int>(m+1));
	for(int i=1;i<=n;++i){
		for(int j=1;j<=m;++j){
			if(s1[i]==s2[j]){
				dp[i][j]=dp[i-1][j-1]+1;
			}
			else{
				dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
			}
		}
	}
	cout<<dp[n][m]<<endl;
	
	return 0;
}
